package moa.sequencestreams;

import java.util.Random;

import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import moa.core.InstancesHeader;
import moa.core.ObjectRepository;
import moa.options.AbstractOptionHandler;
import moa.options.IntOption;
import moa.streams.InstanceStream;
import moa.tasks.TaskMonitor;

public class SequenceGenerator extends AbstractOptionHandler implements
InstanceSequenceStream {

	public IntOption nbCustomerOption = new IntOption("nbCustomer",
			'n', "Number of different customers.", 30);

	public IntOption nbPurchaseOption = new IntOption(
			"nbPurchase", 'p',
			"AVG number of purchase by Transaction.", 2);
	
	public IntOption nbTransactionOption = new IntOption(
			"nbTransaction", 't',
			"AVG number of Transaction of customers.", 3);

	public IntOption maxIntegerOption = new IntOption(
			"maxInteger", 'm',
			"The random number will be between 0 and maxInteger.", 10);
	
	protected InstancesHeader streamHeader;
	
	int cpt = 0;
	
	Attribute atr;

	private Random instanceRandom;
	
	@Override
	protected void prepareForUseImpl(TaskMonitor monitor,
			ObjectRepository repository) {
		monitor.setCurrentActivity("Preparing random sequence values...", -1.0);
		generateHeader();
		restart();	
	}

	private void generateHeader() {
		// TODO Auto-generated method stub
		FastVector attributes = new FastVector();
		FastVector classLabels = null;
		atr = new Attribute("String",classLabels);
		attributes.addElement(atr);
		this.streamHeader = new InstancesHeader(new Instances(
				getCLICreationString(InstanceStream.class), attributes, 0));
		this.streamHeader.setClassIndex(this.streamHeader.numAttributes() - 1);
	}

	@Override
	public long estimatedRemainingInstances() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public InstancesHeader getHeader() {
		// TODO Auto-generated method stub
		return this.streamHeader;
	}

	@Override
	public boolean hasMoreInstances() {
		// TODO Auto-generated method stub
		if (cpt < nbCustomerOption.getValue())
		{
			cpt ++;
			return true;
		}
		else	
			return false;
	}

	@Override
	public boolean isRestartable() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Instance nextInstance() {
		InstancesHeader header = getHeader();
		Instance inst = new Instance(header.numAttributes());
		/*
		for (int i = 0; i < attVals.length; i++) {
		     attVals[i] = "1 2 3 4";
		     inst.setValue(i, attVals[i]);
		  }*/
		String randValue = "";
		Integer val ;
		Random r = new Random();
		for (int cptT = 0; cptT < nbTransactionOption.getValue();cptT++)
		{
			for (int cptP = 0; cptP < nbPurchaseOption.getValue();cptP++)
			{
				val = r.nextInt(maxIntegerOption.getValue());
				randValue = randValue + val.toString();
				randValue = randValue + " ";
			}		
			randValue = randValue + "-1";
		}
		//randValue = randValue + "2 3 4";
		  inst.setValue(atr, randValue );
		  inst.setDataset(header);
		  return inst;
	}

	@Override
	public void restart() {
		// TODO Auto-generated method stub

	}

	@Override
	public void getDescription(StringBuilder sb, int indent) {
		// TODO Auto-generated method stub
		
	}

}
